import navToNext from '../../../wxjs/navToNext.js'
import Paging from '../../../wxjs/Paging.js'
const app = getApp();
var pag = new Paging();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  curType:1,
  keyword1: '',
  navList: [{ name: '报名中', type: 1 }, { name: '我参与', type: 2 }, { name: '我创建', type: 3 }],
};

var currentPage = {
  data,
  reqUrl: app.serAddrs.selfGetList,
  // onLoad,
  cutNav,
  getList,
  confirm,
  callBack,
  // signUp,
  payment,
  onShow,
  toPayPage
};

// 微信支付
function signUp(e) {
  const that = this;
  var challengeId = e.target.dataset.id || 0;
  console.log(challengeId)
  let totalPrice = e.target.dataset.price || 0;
  if (totalPrice == 0 || !challengeId) return false;
  var payParam = {};
  payParam.tV2CustomMatchScheduleId = challengeId;
  payParam.wxMold = 1;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.selfOrderByWXPay,
    data: payParam,
    cancelToast: true,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    success: function (ores) {
      that.payment(ores.data, challengeId);
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })
}
function toPayPage(e) {
  const that = this;
  var challengeId = e.target.dataset.id || 0;
  let totalPrice = e.target.dataset.price || 0;
  if (!challengeId) return false;
  // 判断用户登录
  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;
  myInfo = app.isNavigateToBind();
  if (!myInfo) return;
  let curItem = null;
  this.data.lists.forEach(item => {
    if (item.id == challengeId) {
      curItem = item
    }
  })
  if (!curItem) return false;
  wx.navigateTo({
    url: '/pages/pay/pay?MatchType=2&item=' + JSON.stringify(curItem)
  })
}

var page = Object.assign({}, pag.func(), navToNext, currentPage);

Page(page);

function onLoad(options) {
  const that = this
  app.xlogin.getUser(function (res) {
    var myInfo = app.isNavigateToLogin();
    if (!myInfo) {
      that.setData({
        isLogin: false
      })
      return;
    }

    myInfo = app.isNavigateToBind();
    if (!myInfo) {
      that.setData({
        isLogin: false
      })
      return;
    }
    that.getList();
  })

  that.getList();
}
function onShow() {
  const that = this;
  // app.xlogin.getUser(function (res) {
  //   var myInfo = app.isNavigateToLogin();
  //   if (!myInfo) {
  //     that.setData({
  //       isLogin: false
  //     })
  //     return;
  //   }

  //   myInfo = app.isNavigateToBind();
  //   if (!myInfo) {
  //     that.setData({
  //       isLogin: false
  //     })
  //     return;
  //   }
  // })
  that.getList();
}
function getList(name){
  wx.showLoading({
    title: '加载中',
  })
  const that = this;
  that.listParams.customScheduleType = that.data.curType;
  that.listParams.name = name||'';
  that.setData({
    keyword1: that.listParams.name
  })
  // that.listParams.userId = "466";
  that.pageListInit();
}
function callBack(){
  wx.hideLoading()
}
function confirm(e){
  var value=e.detail.value;
  this.getList(value);
}

function cutNav(e){
  var type=e.target.dataset.id;
  this.setData({
    curType:type
  });
  this.listParams.name = this.data.keyword1;
  this.getList();
}

function payment(data, challengeId) {
  const that = this;

  wx.requestPayment({
    timeStamp: data.timeStamp,
    nonceStr: data.nonceStr,
    package: data.prepayId,
    signType: data.signType,
    paySign: data.paySign,
    success(res) {
      // wx.navigateTo({
      //   url: that.data.xhx_route.selfChallengeDetail + '?uuid=' + challengeId,
      // })
      that.getList();
    },
    fail(res) {
      app.middleware.showToast("支付失败");
    },
    complete: function (res) {

    }
  })
}

